/**
 * Created by zhaojing on 2017/10/30.
 */

/**
 * @file
 * @auth zhaojing
 * @date 2016/11/24
 */

import './style.scss';
import tpl from './tpl.html';

import Vue from 'vue';
import modal from '../../../../../grsc-base/component/Modal.vue';
import Popover from '../../../../../grsc-base/component/Popover.vue';
import Validator from '../../../../../grsc-base/component/Validator/main';
import VerifyCode from '../../../../component/VerifyCode.vue';
import lib from '../../../../common/lib';
import Qrcode from '../../../../../grsc-base/component/Qrcode.vue';
import Loading from '../../../../../grsc-base/component/Loading.vue'

import service from '../../basic/service';

export default  Vue.extend({
    template: tpl,
    props: {
        onBindingSuccess: {
            type: Function,
            default() {
            }
        }
    },
    components: {
        modal,
        Popover,
        VerifyCode,
        Validator,
        Qrcode,
        Loading
    },
    created() {
        this.googleCodeGet();
    },
    mounted() {

    },
    watch: {},
    computed: {},
    data() {
        return {
            googleAuthenticator:'',
            secretcode:'',
            clientName:'smart:'+lib.getAccountNo(),
            form:{
                googleCode:'',
                identifyingCode:''
            },
            rules:{
                googleCode:{
                    required: true,
                    requiredTip: this.$t('common.googleCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.GOOGLE_CODE[this.$i18n.locale]
                },
                identifyingCode: {
                    required: true,
                    requiredTip: this.$t('common.verifyCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.IDENTIFY[this.$i18n.locale]
                },
            },
            isLoading:true,
            isRequesting: false,
        }
    },
    methods: {

        //获取二维码信息
        googleCodeGet(){
            service.googleCodeGet().done(rep=>{
                if (rep.code === window.CODE.SUCCESS) {
                    this.googleAuthenticator = rep.data.googleAuthenticator;
                    this.secretcode = rep.data.secretcode;
                    this.isLoading = false;
                }
            }).always(rep=>{

            })
        },



        submit(){
            if(this.rules.$checkAll()===false){
                return;
            }

            this.isRequesting = true;

            let param = {

            };

            let header = {
                'service-code': this.form.identifyingCode,
                'google-code':this.form.googleCode,
            };

            service.googleCodeResetcheck(param, header).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.hide();
                    window.success(this.$t('common.success'));
                } else {
                    window.error(rep.msg);
                }
            }).always(()=> {
                this.isRequesting = false;
            });
        },

        onSendSuccess() {
            this.rules.identifyingCode.$setRight(this.$t('common.codeSentTip'));
        },
        onSendFail(rep) {
            this.rules.identifyingCode.$setError(rep.msg);
        },

        focusTips(){
            this.rules.password.tip = this.$t('common.ruleAssetPassLength');
        },
        blurTips(){
            this.rules.password.tip = "";
        },
        hide() {
            this.$emit('showResetGoogle');
        }
    }

});
